<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="studentID">
        <el-input v-model="queryParams.studentID" placeholder="请输入学号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="指标" prop="indicatorID">
        <el-input v-model="queryParams.indicatorID" placeholder="请输入指标" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="时间" prop="evaluationDate">
        <el-date-picker clearable v-model="queryParams.evaluationDate" type="month" placeholder="请选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['score:score:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['score:score:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['score:score:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['score:score:export']">导出</el-button>
        <el-button type="warning" plain icon="upload" @click="handleImport"
          v-hasPermi="['score:score:add']">导入</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scoreList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学号" align="center" prop="studentID" />
      <el-table-column label="指标" align="center" prop="indicatorID" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="分数" align="center" prop="score" />
      <el-table-column label="时间" align="center" prop="evaluationDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.evaluationDate, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['score:score:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['score:score:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getbasicMessage()" />

    <!-- 添加或修改学生成绩录入对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="scoreRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学号" prop="studentID">
          <el-input v-model="form.studentID" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="指标" prop="indicatorID">
          <el-input v-model="form.indicatorID" placeholder="请输入指标" />
        </el-form-item>
        <el-form-item label="指标名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入指标名称" />
        </el-form-item>
        <el-form-item label="分数" prop="score">
          <el-input v-model="form.score" placeholder="请输入分数" />
        </el-form-item>
        <el-form-item label="时间" prop="evaluationDate">
          <el-date-picker v-model="form.evaluationDate" type="month" placeholder="选择月份">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 上传文件 -->
    <el-dialog title="上传文件" v-model="excelopen" width="600px" append-to-body>
      <upload />
    </el-dialog>

  </div>
</template>

<script setup name="Score">
import { listScore, getScore, delScore, addScore, updateScore, listStudentByTeacher } from "@/api/score/score";
const { proxy } = getCurrentInstance();
import upload from '@/views/tool/uploadVue/index'
import { ref } from "vue";
// 查询基本信息
import { getInfo } from "@/api/login";
const scoreList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const excelopen = ref(false)
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    studentID: null,
    indicatorID: null,
    score: null,
    evaluationDate: null,
    name: null

  },
  rules: {
    studentID: [
      { required: true, message: "学号不能为空", trigger: "blur" }
    ],
    indicatorID: [
      { required: true, message: "指标不能为空", trigger: "blur" }
    ],
    score: [
      { required: true, message: "分数不能为空", trigger: "blur" }
    ],
    evaluationDate: [
      { required: true, message: "时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);
/** 查询学生成绩录入列表 */
function getList() {
  loading.value = true;
  listScore(queryParams.value).then(response => {
    scoreList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    studentID: null,
    indicatorID: null,
    score: null,
    name: null,
    evaluationDate: null,
    CreatedAt: null,
    UpdatedAt: null
  };
  proxy.resetForm("scoreRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.evaluationDate = dayjs(queryParams.value.evaluationDate).format("YYYY-MM-DD");
  queryParams.value.pageNum = 1;
  console.log(infoType.value)
  console.log(queryParams.value);
  if (infoType.value === 1) getList();
  else if (infoType.value == 0) getListByTeacher();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加学生成绩录入";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getScore(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改学生成绩录入";
  });
}
import dayjs from "dayjs"
/** 提交按钮 */
function submitForm() {
  form.value.evaluationDate = dayjs(form.value.evaluationDate).format("YYYY-MM-DD");
  proxy.$refs["scoreRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateScore(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getbasicMessage();
        });
      } else {
        addScore(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getbasicMessage();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  console.log(row.id);
  const _IDs = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除学生成绩录入编号为"' + _IDs + '"的数据项？').then(function () {
    return delScore(_IDs);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('score/score/export', {
    ...queryParams.value
  }, `score_${new Date().getTime()}.xlsx`)
}

function handleImport() {
  excelopen.value = true
}

function getListByTeacher() {
  scoreList.value=[]
  loading.value = true;
  listStudentByTeacher(teacherId.value, queryParams.value).then(response => {
    console.log(response);
    scoreList.value = response.rows;
    // total.value = response.total;
    loading.value = false;
  })
}
const infoType = ref(0);
const teacherId = ref("")
function getbasicMessage() {
  scoreList.value = []
  getInfo().then(res => {
    const role = res.roles[0]
    if (role == "teacher") {
      teacherId.value = res.user.userName
      infoType.value = 0
      // 获取当前老师下的所有学生的成绩信息
      getListByTeacher()
    } else if (role == "admin") {
      infoType.value = 1
      getList()
    }
  })
}

getbasicMessage()
</script>
